<?php


namespace app\admin\controller\System;


use app\admin\controller\BaseController;
use app\admin\model\System\LogisticsModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\facade\Db;
use think\Request;

class Logistics extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getLogisticsList(Request $request)
    {
        $where = [];
        $param = $request->param();
        $page = $param['page'];
        $limit = $param['limit'];
        $company_name = !empty($param['company_name']) ? $param['company_name'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($page)){
            $page = 1;
        }
        if(empty($limit)){
            $limit = 10;
        }

        if(!empty($company_name)){
            array_push($where,['company_name','like','%'.$company_name.'%']);
        }
        if(!empty($is_show)){
            if($is_show == 1){
                array_push($where,['is_show','=',1]);
            }
            if($is_show == 2){
                array_push($where,['is_show','=',0]);
            }
        }
        $list = LogisticsModel::getLogisticsListWhere($page,$limit,$where);
        $count = LogisticsModel::getLogisticsCountWhere($where);

        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");

    }

    /**
     * 查询更多
     * @param Request $request
     * @return array
     */
    public function getLogisticsMore(Request $request)
    {
        $logistics = LogisticsModel::getLogisticsWhereMore([['is_show','=',1]]);
        return Util::returnJson(self::CODE_DATA,$logistics);
    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addLogistics(Request $request)
    {
        $param = $request->param();
        $company_name = $param['company_name'];
        $com = $param['com'];
        $type = $param['type'];
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";

        $data = [
            'company_name'=>$company_name,
            'com'=>$com,
            'type'=>$type,
            'up_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show)){
            if($is_show == "on"){
                $data['is_show'] = 1;
            }else{
                $data['is_show'] = 0;
            }
        }else{
            $data['is_show'] = 0;
        }
        $id = LogisticsModel::addLogistics($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editLogistics(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $company_name = $param['company_name'];
        $com = $param['com'];
        $type = $param['type'];
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($id)){
            return Util::returnJson(self::CODE_ERROR,null,"参数错误");
        }

        $save = [
            'company_name'=>$company_name,
            'com'=>$com,
            'type'=>$type,
            'up_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show)){
            if($is_show == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }else{
            $save['is_show'] = 0;
        }
        $flag = LogisticsModel::updateLogisticsWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delLogistics(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try{
            //中间逻辑代码
            $flag = LogisticsModel::deleteLogisticsWhere([['id','=',$id]]);
            if($flag){
                //提交事务
                DB::commit();
                return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
            }else{
                DB::rollBack();
                return Util::returnJson(self::CODE_ERROR,null,"删除失败4");
            }
        }catch (\Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            DB::rollBack();
            return Util::returnJson(self::CODE_ERROR,$e,"删除失败1");
        }
    }

}